package bb.lanxing.mvp.presetner;

import android.content.Context;
import android.os.Bundle;
import bb.lanxing.devices.sync.XossGHistoryManager;
import bb.lanxing.lib.devices.core.sync.SyncService;
import bb.lanxing.lib.devices.sprint.model.SprintFirmwareModel;
import bb.lanxing.lib.devices.sprint.model.SprintHomeModel;
import bb.lanxing.lib.devices.sprint.presenter.impl.AbstractSprintHomePresenter;
import bb.lanxing.lib.devices.sprint.view.SprintHomeView;
import bb.lanxing.lib.devices.utils.DeviceVersionHelper;
import bb.lanxing.mvp.model.XOSSGSprintServerFirmwareModel;
import bb.lanxing.mvp.model.XZSprintHomeModel;
import bb.lanxing.service.AppSyncService;
import rx.functions.Action1;

public class XossGPresenterImpl extends AbstractSprintHomePresenter {
    @Override
    protected SprintFirmwareModel createSprintFirmwareModel() {
        return null;
    }

    @Override
    public Bundle getArguments() {
        return null;
    }

    public XossGPresenterImpl(SprintHomeView sprintHomeView) {
        super(sprintHomeView, new XZSprintHomeModel(sprintHomeView.getContext()));
        initXossGSubscriber(this.sprintModel, sprintHomeView);
    }

    void initXossGSubscriber(SprintHomeModel gModel, final SprintHomeView view) {
        gModel.subscribeSoftwareVersion(new Action1<String>() {
            @Override
            public void call(String s) {
                XossGPresenterImpl.this.sw = s;
                XossGPresenterImpl.this.connectDeviceIfNecessary();
                if (s != null) {
                    DeviceVersionHelper.setLocalSoftwareVersion(view.getContext(), XossGPresenterImpl.this.getAddress(), s);
                    view.setSoftware(s);
                }
            }
        });
        gModel.subscribeModel(new Action1<String>() {
            @Override
            public void call(String s) {
                XossGPresenterImpl.this.model = s;
                XossGPresenterImpl.this.connectDeviceIfNecessary();
                XossGPresenterImpl xossGPresenterImpl = XossGPresenterImpl.this;
                xossGPresenterImpl.requestXossGFirmwareVersion(xossGPresenterImpl.model, XossGPresenterImpl.this.hw);
                SprintHomeView sprintHomeView = view;
                if (sprintHomeView != null) {
                    sprintHomeView.setModel(s);
                    if (s != null) {
                        DeviceVersionHelper.setModelVersion(XossGPresenterImpl.this.getContext(), XossGPresenterImpl.this.getAddress(), s);
                    }
                }
            }
        });
        gModel.subscribeHardwareVersion(new Action1<String>() {
            @Override
            public void call(String s) {
                XossGPresenterImpl.this.hw = s;
                XossGPresenterImpl xossGPresenterImpl = XossGPresenterImpl.this;
                xossGPresenterImpl.requestXossGFirmwareVersion(xossGPresenterImpl.model, XossGPresenterImpl.this.hw);
                SprintHomeView sprintHomeView = view;
                if (sprintHomeView != null) {
                    sprintHomeView.setHardware(s);
                    DeviceVersionHelper.setHwVersion(XossGPresenterImpl.this.getContext(), XossGPresenterImpl.this.getAddress(), XossGPresenterImpl.this.hw);
                }
                XossGPresenterImpl.this.connectDeviceIfNecessary();
            }
        });
    }

    @Override
    protected SprintFirmwareModel createXOSSFirmwareModel(String model, String hardware) {
        return new XOSSGSprintServerFirmwareModel(model, hardware);
    }

    @Override
    public String getSyncManagerName() {
        return XossGHistoryManager.class.getName();
    }

    @Override
    public Class<? extends SyncService> getSyncService() {
        return AppSyncService.class;
    }

    @Override
    public Context getContext() {
        if (getView() != null) {
            return getView().getContext();
        }
        return null;
    }

    @Override
    public void unboundDevice() {
        disconnectDeviceIfNecessary();
    }
}
